package com.haiyuancloud.base.ui.ctr;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.haiyuancloud.base.deal.core._enum.DealBaseEnum;
import com.haiyuancloud.base.deal.core.chain.enter.UiPageLogPushFactory;
import com.haiyuancloud.base.deal.ctr.BaseRequestCtr;
import com.haiyuancloud.base.ui.page.core.chain.model.UiAppTplModel;
import com.haiyuancloud.base.ui.page.core.chain.plugin.UiPageAppTplChainPlugin;
import com.haiyuancloud.base.ui.page.core.chain.plugin.UiPageSessionTplChainPlugin;
import com.haiyuancloud.base.ui.product.engine.RunUiEnginePlugin;
import com.haiyuancloud.base.ui.product.engine.UiPageEngineService;
import com.haiyuancloud.base.ui.product.engine.UiProductDevStaffPlugin;
import com.haiyuancloud.base.ui.product.engine.model.RunUiPageModel;
import com.haiyuancloud.common.core.entity.CurrentUser;
import com.haiyuancloud.common.core.entity.constant.HaiyuanConstant;
import com.haiyuancloud.common.core.utils.HaiyuanUtil;
import com.haiyuancloud.common.core.utils.SessionDataUtils;
import com.haiyuancloud.base.ui.page.core.chain.UiPageChainPluginFactory;
import com.haiyuancloud.common.redis.service.CaffeineCacheService;
import com.haiyuancloud.db.core.model.DsDataModel;
import com.haiyuancloud.util.ObjectUtil;
import lombok.extern.log4j.Log4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Log4j
@RestController
@RequestMapping("/ui/ctr")
public class UICtrl extends BaseRequestCtr {

    @Resource
    private UiPageLogPushFactory uiPageLogPushFactory;

    @Resource
    private UiPageAppTplChainPlugin uiPageAppTplChainPlugin;

    @Resource
    private UiPageSessionTplChainPlugin uiPageSessionTplChainPlugin;

    @Resource
    private UiPageEngineService uiPageEngineService;

    @PostMapping("/initSessionFormData")
    @ResponseBody
    public Map<String, Object> initSessionFormData(@RequestBody(required=false) Map<String, Object> submitMap, HttpServletRequest request) {
        Map<String, Object> data = getUiResultData();
        DsDataModel dsDataModel = SessionDataUtils.getDsDataModel();
        if (ObjectUtil.isNull(submitMap)) {
            submitMap = new HashMap<>();
        }
        Map<String,Object> sessionTplData = uiPageSessionTplChainPlugin.chainSessionFormData(dsDataModel, submitMap);
        data.put("data", sessionTplData);
        return data;
    }


    @PostMapping("/initSessionUIData")
    @ResponseBody
    public Map<String, Object> initSessionUIData(@RequestBody(required=false) Map<String, Object> submitMap, HttpServletRequest request) {
        Map<String, Object> data = getUiResultData();
        DsDataModel dsDataModel = SessionDataUtils.getDsDataModel();
        if (ObjectUtil.isNull(submitMap)) {
            submitMap = new HashMap<>();
        }
        Map<String,Object> sessionTplData = uiPageSessionTplChainPlugin.chainSessionUiData(dsDataModel, submitMap);
        data.put("data", sessionTplData);
        return data;
    }

    @PostMapping("/initAppUIData/{S_PARTITION}/{S_PAGE_ID}/{S_PAGE_VER}")
    @ResponseBody
    public Map<String, Object> initAppUIData(
            @PathVariable(value = "S_PARTITION") Integer S_PARTITION
            , @PathVariable(value = "S_PAGE_ID") String S_PAGE_ID
            , @PathVariable(value = "S_PAGE_VER") String S_PAGE_VER
            , @RequestBody(required=false) Map<String, Object> submitMap, HttpServletRequest request) {
        Map<String, Object> data = getUiResultData();
        Map<String, Object> uiData = new HashMap<>();
        List<UiAppTplModel>  appTplModelList = new ArrayList<>();
        if(ObjectUtil.isNotNull(submitMap.get("app"))) {
            DsDataModel dsDataModel = SessionDataUtils.getDsDataModel();
            dsDataModel.setOPEN_PARTITION(S_PARTITION);
            JSONArray app = (JSONArray)submitMap.get("app");
            appTplModelList = app.toJavaList(UiAppTplModel.class);
            log.debug("查询应用模板数据@"+JSON.toJSONString(appTplModelList));
            Map<String,Object> appTplData = uiPageAppTplChainPlugin.chain(dsDataModel,S_PAGE_ID,S_PAGE_VER,appTplModelList,submitMap);
            uiData.put("appData", appTplData);

            Map<String,Object> sessionTplData = uiPageSessionTplChainPlugin.chain(dsDataModel,S_PAGE_ID,S_PAGE_VER,appTplModelList,submitMap);
            uiData.put("sessionData", sessionTplData);
        }
        data.put("data",uiData);
        return data;
    }

    @PostMapping("/initComonUIData/{S_PARTITION}/{S_PAGE_ID}/{S_PAGE_VER}")
    @ResponseBody
    public Map<String, Object> initComonUIData(
            @PathVariable(value = "S_PARTITION") Integer S_PARTITION
            , @PathVariable(value = "S_PAGE_ID") String S_PAGE_ID
            , @PathVariable(value = "S_PAGE_VER") String S_PAGE_VER
            , @RequestBody(required=false) Map<String, Object> submitMap, HttpServletRequest request) {
        Map<String, Object> data = getUiResultData();
        String resultCode = "1";
        String resultMsg = "加载成功";
        long start = System.currentTimeMillis();
        if (ObjectUtil.isNull(submitMap)) {
            submitMap = new HashMap<>();
        }
        DsDataModel dsDataModel = SessionDataUtils.getDsDataModel();
        dsDataModel.setOPEN_PARTITION(S_PARTITION);
        CurrentUser currentUser = HaiyuanUtil.getCurrentUser();
        String productId = currentUser.getProductId();
        String S_LAYOUT_ID = ObjectUtil.toString(currentUser.getLayoutId(), HaiyuanConstant.I_LAYOUT_ID);
        try {
            RunUiPageModel runUiPageModel = uiPageEngineService.getUiPageData( dsDataModel,  currentUser,  S_PAGE_ID,  S_PAGE_VER, S_PARTITION,  S_LAYOUT_ID,  productId, submitMap);
            if (DealBaseEnum.resultCode.DEFAULT.getValue().equals(runUiPageModel.getResultCode())){
                data.put("success", true);
            }else{
                data.put("success", false);
            }
            resultCode = runUiPageModel.getResultCode();
            resultMsg = runUiPageModel.getResultMsg();
            data.put("message", resultMsg);
            data.put("printMsg", runUiPageModel.getErrorLog());
            data.put("data", runUiPageModel.getPageObj());
            data.put("S_PRODUCT_ID",productId);
            data.put("S_PAGE_ID", S_PAGE_ID);
            data.put("S_PAGE_VER", S_PAGE_VER);
            data.put("S_PARTITION", S_PARTITION);
       }catch (Exception e){
            resultCode = "2";
            resultMsg = "操作异常";
            data.put("printMsg", e.getMessage());
          throw  e;
       }finally {
          long end = System.currentTimeMillis();
          uiPageLogPushFactory.log(dsDataModel,S_PAGE_ID,S_PAGE_VER,null,start,end,resultCode,resultMsg,submitMap);
       }
        return data;
    }
}
